package hieuph.facerecognition.ManipulationPicture;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;

public class PictureUtility {
	public final static int CAMERA_RETURN_CODE = 0;
	public final static int OPEN_PICTURE_RETURN_CODE = 1;
	public final static int PIC_CROP = 2;

	public static void takePicture(Context context) {
		Intent cameraIntent = new Intent(
				android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
		((FragmentActivity) context).startActivityForResult(cameraIntent,
				CAMERA_RETURN_CODE);
	}

	public static void openPicture(Context context) {
		Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
		intent.setType("image/*");
		((FragmentActivity) context).startActivityForResult(intent,
				OPEN_PICTURE_RETURN_CODE);
	}

	public static void performCrop(Context mContext, Uri picUri) {
		// take care of exceptions
		try {
			// call the standard crop action intent (the user device may not
			// support it)
			Intent cropIntent = new Intent("com.android.camera.action.CROP");
			// indicate image type and Uri
			cropIntent.setDataAndType(picUri, "image/*");
			// set crop properties
			cropIntent.putExtra("crop", "true");
			// indicate aspect of desired crop
			cropIntent.putExtra("aspectX", 1);
			cropIntent.putExtra("aspectY", 1);
			// indicate output X and Y
			cropIntent.putExtra("outputX", 256);
			cropIntent.putExtra("outputY", 256);
			// retrieve data on return
			cropIntent.putExtra("return-data", true);
			// start the activity - we handle returning in onActivityResult
			((FragmentActivity) mContext).startActivityForResult(cropIntent,
					PIC_CROP);
		}
		// respond to users whose devices do not support the crop action
		catch (ActivityNotFoundException anfe) {
			// display an error message
			String errorMessage = "Whoops - your device doesn't support the crop action!";
			Toast toast = Toast.makeText(mContext, errorMessage,
					Toast.LENGTH_SHORT);
			toast.show();
		}
	}
}
